'''
Created on Jul 30, 2012

@author: peng
'''
import os

key_dict = {}
new_count_dict = {}
upgrade_count_dict = {}
active_count_dict = {}
launch_times_dict = {}
t_value_dict = {}
t_count_dict = {}
use_times_dict = {}
total_count_dict = {}
create_time_dict = {}
update_time_dict = {}

_dict_value_map = {6: new_count_dict,
                   7: upgrade_count_dict,
                   8: active_count_dict,
                   9: launch_times_dict,
                   10: t_value_dict,
                   11: t_count_dict,
                   12: use_times_dict,
                   13: total_count_dict}

_dict_time_map = {14: create_time_dict,
                  15: update_time_dict}

_base_path = '/home/ubuntu'

_folder = _base_path + '/merge/input/language_daily'
_output_file = _base_path + '/merge/output/language_daily.out'

if __name__ == '__main__':
    key_index = 0
    count = 0
    files = os.listdir(_folder)
    for f in files:
        if f.endswith('~'):
            continue
        path = os.path.join(_folder, f)
        in_file = open(path)
        print 'file [%s]' % path
        for l in in_file.readlines():
            count += 1
            if count == 10000:
                count = 0
                print 'W',

            l = l.replace('\n', '').split('\t')
            key = '\t'.join(l[2:6])
            key = key.lower()
            if key not in key_dict:
                key_dict[key] = key_index
                key_index += 1
            for d in sorted(_dict_value_map.keys()):
                if key_dict[key] not in _dict_value_map[d]:
                    _dict_value_map[d][key_dict[key]] = int(l[d])
                else:
                    _dict_value_map[d][key_dict[key]] += int(l[d])

            for d in sorted(_dict_time_map.keys()):
                if key_dict[key] not in _dict_time_map[d]:
                    _dict_time_map[d][key_dict[key]] = l[d]

        in_file.close()

    print 'key index [%s]' % key_index

    if os.path.exists(_output_file):
        os.remove(_output_file)

    out_file = open(_output_file, 'w')
    for key in sorted(key_dict.keys()):
        line = '6e1cd3bc2cda139a29b552ae5d742b80\t' + key
        for d in sorted(_dict_value_map.keys()):
            line += '\t' + str(_dict_value_map[d][key_dict[key]])

        for d in sorted(_dict_time_map.keys()):
            line += '\t' + str(_dict_time_map[d][key_dict[key]])
        out_file.write(line + '\n')
    out_file.close()
